package cn.cvs.servlet.sysUser;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "ExistServlet", urlPatterns = "/sys/user/userExist")
public class ExistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SysUserService sysUserService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        String account = request.getParameter ( "account" );
        SysUser user = sysUserService.getUserByAccountAndPwd ( account, null );
        Map map = new HashMap<> ( );
        if (user != null) {
            map.put ( "exist", 1 );
        } else {
            map.put ( "exist", 0 );
        }
        PrintWriter writer = response.getWriter ( );
        writer.print ( JSON.toJSONString ( map ) );
        writer.close ();
    }
}
